<?php
/**
 * @desc 采购计划管理
 * @author kevin wang
 * @date 2014-09-20
 */
class ctrl_purch extends ctrl_base {
    private static $dirName = 'purch';
    /**
     * @desc 采购计划列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('purch_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$user_company = isset($_POST['user_company']) ? trim($_POST['user_company']) : '';
    	$user_person = isset($_POST['user_person'])? trim($_POST['user_person']):'';
    	$product_type = isset($_POST['product_type'])? trim($_POST['product_type']):'';
    	//搜索条件组合
		$condition = array();
    	if(!empty($user_company)) {
    		$condition['user_company'] = $user_company;
    	}
		if(!empty($user_person)) {
    		$condition['user_person'] = $user_person;
    	}
		if(!empty($product_type)) {
    		$condition['product_type'] = $product_type;
    	}
		//订单获取
    	$purch_list = model_purch :: purchList($nextpage,$pagesize,$condition);
    	$total = array_pop($purch_list);
		//模板赋值
    	self::smarty() -> assign('purch_list', $purch_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('user_company', $user_company);
		self::smarty() -> assign('user_person', $user_person);
		self::smarty() -> assign('product_type', $product_type);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/purch_list.html');    	
    }
	//添加采购计划
	public static function purchAdd(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('purch_add'));
		if($_POST){

			$purch = self::get_request_data();
			$purch['author'] = $_SESSION['admin']['username'];
			$purch['editer'] = $_SESSION['admin']['username'];

			$result = model_purch::purchAdd($purch);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','purch_main','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}
		}
		self::smarty() -> display(self::$dirName . '/purch_add.html');
	}
	//删除采购计划
	public static function purchDelete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('purch_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$id[] = intval($v);
		}
    	$data = array('delete_flag' => 1);

    	$st = model_purch::updatePurchInfo($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','purch_main');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑采购计划
	 public static function purchEdit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$purch = model_purch::getPurchInfoById($id);
		$ctrl_tag = 'purch_edit_other';
		if($order['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'purch_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$purch = array();

			$purch = self::get_request_data();
			$purch['editer'] = $_SESSION['admin']['username'];
			unset($purch['id']);

			$result = model_purch::updatePurchInfo(explode(',',$id), $purch);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','purch_main','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('purch', $purch);
    	self::smarty() -> display(self::$dirName . '/purch_edit.html');
    }
}
